<?php defined('SYSPATH') or die(); ?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>优惠策略管理-思途CMS{$coreVersion}</title>
    {template 'stourtravel/public/public_js'}
    {php echo Common::getCss('style.css,base.css,base_new.css'); }
    {php echo Common::getScript("uploadify/jquery.uploadify.min.js,jquery.buttonbox.js,choose.js"); }

</head>
<body style="overflow:hidden">

<table class="content-tab">
    <tr>
        <td width="119px" class="content-lt-td" valign="top">
            {template 'stourtravel/public/leftnav'}
            <!--右侧内容区-->
        </td>
        <td valign="top" class="content-rt-td" style="overflow:hidden">
            <div class="cfg-header-bar">
                <div class="cfg-header-search">
                    <select onchange="search_list($(this))" style="height: 30px" size="1" class="select w100 fl mr-5">
                        <option value="0">策略类型</option>
                        <option value="1">连住策略</option>
                        <option value="2">量返策略</option>
                    </select>
                    <input type="text" id="searchkey" value="策略名称" class="search-text">
                    <a href="javascript:;" class="search-btn" onclick="CHOOSE.searchKeyword()">搜索</a>
                </div>
                <a href="javascript:;" class="fr btn btn-primary radius mt-6 mr-10" onclick="window.location.reload()">刷新</a>
                <a href="javascript:;" class="fr btn btn-primary radius mt-6 mr-10" id="addbtn">添加</a>
            </div>
            <div id="product_grid_panel" class="content-nrt">

            </div>
        </td>
    </tr>
</table>

<script>
    function search_list(obj)
    {
        var val = obj.val();
        window.product_store.getProxy().setExtraParam('strategy_type',val);
        window.product_store.loadPage(1);
    }

    window.display_mode = 1;	//默认显示模式
    window.product_kindid = 0;  //默认目的地ID


    Ext.onReady(
        function () {
            Ext.tip.QuickTipManager.init();

            $(".btnbox").buttonBox();

            $("#searchkey").focusEffect();
            //添加按钮
            $("#addbtn").click(function () {
                ST.Util.addTab('添加优惠策略', '{$cmsurl}ebooking/discount_add_or_edit/{if isset($_GET['menuid'])}menuid/{$_GET['menuid']}/{/if}', 0);
            });


            //产品store
            window.product_store = Ext.create('Ext.data.Store', {

                fields: [
                    'id',
                    'name',
                    'type',
                    'value_type',
                    'value',
                    'is_open',
                    'create_time',
                    'type_id',
                    'msg'
                ],

                proxy: {
                    type: 'ajax',
                    api: {
                        read: SITEURL + 'ebooking/discount_strategy/action/read',  //读取数据的URL
                        update: SITEURL + 'ebooking/discount_strategy/action/save',
                        destroy: SITEURL + 'ebooking/discount_strategy/action/delete'
                    },
                    reader: {
                        type: 'json',   //获取数据的格式
                        root: 'lists',
                        totalProperty: 'total'
                    }
                },
                remoteSort: true,
                autoLoad: true,
                pageSize: 20,
                listeners: {
                    load: function (store, records, successful, eOpts) {
                        if (!successful) {
                            ST.Util.showMsg("{__('norightmsg')}", 5, 1000);
                        }
                        var pageHtml = ST.Util.page(store.pageSize, store.currentPage, store.getTotalCount(), 10);
                        $("#line_page").html(pageHtml);
                        window.product_grid.doLayout();

                        $(".pageContainer .pagePart a").click(function () {
                            var page = $(this).attr('page');
                            product_store.loadPage(page);
                        });
                    }
                }


            });

            //产品列表
            window.product_grid = Ext.create('Ext.grid.Panel', {
                store: product_store,
                renderTo: 'product_grid_panel',
                border: 0,
                bodyBorder: 0,
                bodyStyle: 'border-width:0px',
                scroll: 'vertical', //只要垂直滚动条
                bbar: Ext.create('Ext.toolbar.Toolbar', {
                    store: product_store,  //这个和grid用的store一样
                    displayInfo: true,
                    emptyMsg: "",
                    items: [
                        {
                            xtype: 'panel',
                            id: 'listPagePanel',
                            html: '<div id="line_page"></div>'
                        },
                        {
                            xtype: 'combo',
                            fieldLabel: '每页显示数量',
                            width: 170,
                            labelAlign: 'right',
                            forceSelection: true,
                            value: 20,
                            store: {fields: ['num'], data: [{num: 20}, {num: 40}, {num: 60}]},
                            displayField: 'num',
                            valueField: 'num',
                            listeners: {
                                select: CHOOSE.changeNum
                            }
                        }

                    ],
                    listeners: {
                        single: true,
                        render: function (bar) {
                            var items = this.items;
                            //bar.down('tbfill').hide();

                            bar.insert(0, Ext.create('Ext.panel.Panel', {
                                border: 0,
                                html: '<div class="panel_bar"><a class="btn btn-primary radius" href="javascript:void(0);" onclick="CHOOSE.chooseAll()">全选</a><a class="btn btn-primary radius ml-10" href="javascript:void(0);" onclick="CHOOSE.chooseDiff()">反选</a><a class="btn btn-primary radius ml-10" href="javascript:void(0);" onclick="CHOOSE.delMore()">删除</a></div>'
                            }));

                            bar.insert(2, Ext.create('Ext.toolbar.Fill'));
                            //items.add(Ext.create('Ext.toolbar.Fill'));
                        }
                    }
                }),
                columns: [
                    {
                        text: '选择',
                        width: '5%',
                        tdCls: 'product-ch',
                        align: 'center',
                        dataIndex: 'id',
                        border: 0,
                        menuDisabled: true,
                        renderer: function (value, metadata, record) {
                            id = record.get('id');
                            if (id.indexOf('suit') == -1)
                                return "<input type='checkbox' class='product_check' id='box_" + id + "' style='cursor:pointer' value='" + value + "'/>";
                        }

                    },
                    {
                        text: '策略名称',
                        width: '35%',
                        dataIndex: 'name',
                        align: 'left',
                        menuDisabled: true,
                        border: 0,
                        sortable: false,
                        renderer: function (value, metadata, record) {
                            return value;
                        }
                    },
                    {
                        text: '策略描述',
                        width: '25%',
                        align: 'left',
                        dataIndex: 'msg',
                        border: 0,
                        menuDisabled: true,
                        sortable: false,
                        renderer: function (value, metadata, record, rowIndex, colIndex) {
                            return value;
                        }
                    },
                    {
                        text: '更新时间',
                        width: '15%',
                        align: 'center',
                        border: 0,
                        dataIndex: 'create_time',
                        menuDisabled: true,
                        sortable: false,
                        renderer: function (value, metadata, record) {
                            return value;
                        }
                    },
                    {
                        text: '开启/关闭',
                        width: '10%',
                        align: 'center',
                        border: 0,
                        dataIndex: 'is_open',
                        xtype: 'actioncolumn',
                        menuDisabled: true,
                        cls: 'mod-1 sort-col',
                        items: [
                            {
                                getClass: function (v, meta, rec) {
                                    if (v == 1)
                                        return 'dest-status-ok';
                                    else
                                        return 'dest-status-none';
                                },
                                handler: function (view, index, colindex, itm, e, record) {
                                    var val = record.get('is_open');
                                    var newval = val == 1 ? 0 : 1;
                                    updateField(null, record.get('id'), 'is_open', newval)
                                }
                            }
                        ]
                    },
                    {
                        text: '管理',
                        width: '10%',
                        align: 'center',
                        border: 0,
                        sortable: false,
                        menuDisabled: true,
                        renderer: function (value, metadata, record) {
                            var id = record.get('id');
                            return "<a href='javascript:void(0);' title='编辑' class='btn-link' onclick=\"goModify(" + id + ")\">编辑</a>";
                        }

                    }
                ],
                listeners: {
                    boxready: function () {
                        var height = Ext.dom.Element.getViewportHeight();
                        this.maxHeight = height - 40;
                        this.doLayout();
                    },

                },
                plugins: [
                    Ext.create('Ext.grid.plugin.CellEditing', {
                        clicksToEdit: 2,
                        listeners: {
                            edit: function (editor, e) {
                                var id = e.record.get('id');
                                updateField(0, id, e.field, e.value, 0);
                                return false;
                            },
                            beforeedit: function (editor, e) {

                            }
                        }
                    })
                ],
                viewConfig: {
                }
            });


        })

    //实现动态窗口大小
    Ext.EventManager.onWindowResize(function () {
        var height = Ext.dom.Element.getViewportHeight();
        var data_height = window.product_grid.getView().getEl().down('.x-grid-table').getHeight();
        if (data_height > height - 40)
            window.product_grid.height = (height - 40);
        else
            delete window.product_grid.height;
        window.product_grid.doLayout();


    })

    //更新某个字段
    function updateField(ele, id, field, value, type, callback) {
        var record = window.product_store.getById(id.toString());
        if (type == 'select' || type == 'input') {
            value = Ext.get(ele).getValue();
        }
        var view_el = window.product_grid.getView().getEl();


        Ext.Ajax.request({
            url: SITEURL + "ebooking/discount_strategy/action/update",
            method: "POST",
            datatype: "JSON",
            params: {id: id, field: field, val: value, kindid: window.product_kindid},
            success: function (response, opts) {
                if (response.responseText == 'ok') {

                    record.set(field, value);
                    record.commit();
                    if (typeof (callback) == 'function') {
                        callback(record);
                    }

                } else {
                    ST.Util.showMsg("{__('norightmsg')}", 5, 1000);
                }
            }
        });
    }

    //修改
    function goModify(id) {
        var record = window.product_store.getById(id.toString());
        var title = record.get('name');
        var url = SITEURL + 'ebooking/discount_add_or_edit/{if isset($_GET['menuid'])}menuid/{$_GET['menuid']}/{/if}id/' + id;

        parent.window.addTab('修改-' + title, url, 1);
    }
</script>
</body>
</html>
